class Solution {

    public static int lastStoneWeight(int[] stones){
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < stones.length; i++) {
            maxHeap.offer(stones[i]);
        }

        while(maxHeap.size() > 1){
            int y = maxHeap.poll();
            int x = maxHeap.poll();
            if(x != y){
                maxHeap.offer(y-x);
            }
        }
        if(maxHeap.size() == 0){
            return 0;
        }
        return maxHeap.poll();
    }
}




class Solution {
    public PriorityQueue<Integer> Top(ArrayList<Integer> list){
        PriorityQueue<Integer> minHeap = new PriorityQueue();
        for (int i = 0; i < list.size(); i++) {
            if(minHeap.size() < 2){
                minHeap.offer(list.get(i));
            }else{
                int value = minHeap.peek();
                if(list.get(i)> value){
                    minHeap.poll();
                    minHeap.offer(list.get(i));
                }
            }
        }
        return minHeap;
    }
    public int lastStoneWeight(ArrayList<Integer> list){
        while(list.size() > 1) {
            PriorityQueue minHeap = Top(list);
            int x = (int) minHeap.poll();
            int y = (int) minHeap.poll();
            list.remove(list.indexOf(x));
            list.remove(list.indexOf(y));
            if(x != y){
                list.add(y - x);
            }
        }
        if(list.size() == 0){
            return 0;
        }
        return list.get(0);
    }
    public int lastStoneWeight(int[] stones) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < stones.length; i++) {
            list.add(stones[i]);
        }
        return lastStoneWeight(list);
    }
}